package com.github.itdachen.service.impl;

import com.github.itdachen.entity.ProtoTable;
import com.github.itdachen.framework.context.exception.BizException;
import com.github.itdachen.framework.core.response.TableData;
import com.github.itdachen.mapper.IMySqlTableSourceMapper;
import com.github.itdachen.mapper.IOracleTableSourceMapper;
import com.github.itdachen.sdk.query.ProtoTableQuery;
import com.github.itdachen.service.IDevTableSourceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * MySqlTableSourceServiceImpl
 *
 * @author 王大宸
 * @date 2024/4/3 22:11
 */
@Service
public class DevTableSourceServiceImpl implements IDevTableSourceService {

    private final IMySqlTableSourceMapper mySqlTableSourceMapper;
    private final IOracleTableSourceMapper oracleTableSourceMapper;

    @Value("${pagehelper.helperDialect:mysql}")
    private String tableSourceType;

    public DevTableSourceServiceImpl(IMySqlTableSourceMapper mySqlTableSourceMapper,
                                     IOracleTableSourceMapper oracleTableSourceMapper) {
        this.mySqlTableSourceMapper = mySqlTableSourceMapper;
        this.oracleTableSourceMapper = oracleTableSourceMapper;
    }


    @Override
    public TableData<ProtoTable> findProtoList(ProtoTableQuery query) throws Exception {
        Page<ProtoTable> page = PageHelper.startPage(query.getPage(), query.getLimit());
        List<ProtoTable> list = new ArrayList<>();
        if ("oracle".equals(tableSourceType)) {
            list = oracleTableSourceMapper.findProtoList(query);
            return new TableData<ProtoTable>(page.getTotal(), list);
        }
        list = mySqlTableSourceMapper.findProtoList(query);
        return new TableData<ProtoTable>(page.getTotal(), list);
    }

    @Override
    public List<ProtoTable> findTableInfo(String tableNames) throws Exception {
        if (null == tableNames) {
            throw new BizException("请选择表");
        }
        List<String> strings = new ArrayList<>(Arrays.asList(tableNames.split(",")));
        if (strings.isEmpty()) {
            throw new BizException("请选择表");
        }


        if ("oracle".equals(tableSourceType)) {
            List<ProtoTable> tableList = oracleTableSourceMapper.findProtoTableList(strings);
            if (null == tableList || tableList.isEmpty()) {
                throw new BizException("该表不存在");
            }
            for (ProtoTable protoTable : tableList) {
                protoTable.setColumns(oracleTableSourceMapper.findProtoTableColumns(protoTable.getTableName()));
            }
            return tableList;
        }

        List<ProtoTable> tableList = mySqlTableSourceMapper.findProtoTableList(strings);
        if (null == tableList || tableList.isEmpty()) {
            throw new BizException("该表不存在");
        }
        for (ProtoTable protoTable : tableList) {
            protoTable.setColumns(mySqlTableSourceMapper.findProtoTableColumns(protoTable.getTableName()));
        }
        return tableList;
    }


}
